﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyThuVien.Business;

namespace QuanLyThuVien.Presentation
{
    public partial class frmQuanLyTheThuVien : Form
    {
        public frmQuanLyTheThuVien()
        {
            InitializeComponent();
        }

        TheBUS theBUS = new TheBUS();

        TheThuVien the = new TheThuVien();

        DocGiaBUS DGBus = new DocGiaBUS();

        private void frmQuanLyTheThuVien_Load(object sender, EventArgs e)
        {
            theBUS.LoadLenDataGridView(dgThe);
            DGBus.LayMaDGLenCB(cbMaDG);
        }

        private void btnNhapThe_Click(object sender, EventArgs e)
        {
            try
            {
                the.MaThe = txtMaThe.Text;
                the.MaDG = cbMaDG.Text;
                the.NgayCap = mkNgayCap.Text;
                the.Han = mkHan.Text;
                theBUS.ThemMoiThe(the);
                theBUS.LoadLenDataGridView(dgThe);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "QuanLyThuVien");
            }
        }

        private void btnTaoMoiThe_Click(object sender, EventArgs e)
        {
            cbMaDG.Text = "";
            lbTenDG.Text = "";
            cbChucVuT.Text = "";
            txtMaThe.Text = "";
            mkNgayCap.Text = "";
            mkHan.Text = "";
        }

        private void btnSuaThe_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thay đổi thông tin không?", "QuanLyThuvien", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                the.MaThe = txtMaThe.Text;
                the.MaDG = cbMaDG.Text;
                the.NgayCap = mkNgayCap.Text;
                the.Han = mkHan.Text;
                theBUS.SuaThe(the);
                theBUS.LoadLenDataGridView(dgThe);
            }
        }

        private void btnXoaThe_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa thông tin không?", "QuanLyThuvien", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                theBUS.XoaThe(txtMaThe.Text);
                theBUS.LoadLenDataGridView(dgThe);
            }
        }

        private void dgThe_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int a = e.RowIndex;
            cbMaDG.Text = dgThe[0, a].Value.ToString();
            lbTenDG.Text = dgThe[1, a].Value.ToString();
            cbChucVuT.Text = dgThe[2, a].Value.ToString();
            txtMaThe.Text = dgThe[3, a].Value.ToString();
            mkNgayCap.Text = dgThe[4, a].Value.ToString();
            mkHan.Text = dgThe[5, a].Value.ToString();
        }

        private void cbMaDG_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbTenDG.Text = DGBus.LayTenDGLenLable(cbMaDG.Text);
            cbChucVuT.Text = DGBus.LayChucVuLenCB(cbMaDG.Text);
        }
    }
}
